﻿CREATE PROCEDURE [dbo].[CreatePostData]
	@Domain NVARCHAR(255),
	@BannedBy NVARCHAR(255), 
	@Subreddit NVARCHAR(255), 
	@SelfTextHtml NVARCHAR(500), 
	@SelfText NVARCHAR(255), 
	@Likes INT, 
	@SecureMedia BIGINT, 
	@Saved BIT,
	@ID NVARCHAR(255),
	@SecureMediaEmbed BIGINT, 
	@Clicked BIT,
	@Stickied BIT,
	@Author NVARCHAR(255),
	@Score INT,
	@ApprovedBy NVARCHAR(255), 
	@Over18 BIT,
	@Hidden BIT,
	@Thumbnail NVARCHAR(500),
	@SubredditID NVARCHAR(255),
	@Edited BIT,
	@LinkFlairCssClass NVARCHAR(500), 
	@AuthorFlairCssClass NVARCHAR(500), 
	@Downs INT,
	@IsSelf BIT,
	@Permalink NVARCHAR(1000),
	@Name NVARCHAR(255),
	@Created DATETIME,
	@URL NVARCHAR(1000), 
	@AuthorFlairText NVARCHAR(500), 
	@Title NVARCHAR(1000),
	@CreatedUTC DATETIME,
	@LinkFlairText NVARCHAR(500), 
	@Ups INT,
	@NumComments INT,
	@NumReports INT,
	@Distinguished BIT,
	@MediaEmbed BIGINT,
	@Media BIGINT
AS
BEGIN
	BEGIN TRY
		SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
		SET NOCOUNT ON;
		DECLARE @tmp TABLE (pk BIGINT);
		DECLARE @datacounter INT;
		DECLARE @TID BIGINT;
		DECLARE @report XML;

		INSERT INTO [Raw].[vwPostData] (Domain,
										BannedBy,
										Subreddit,
										SelfTextHtml,
										SelfText,
										Likes,
										SecureMedia,
										Saved,
										ID,
										SecureMediaEmbed,
										Clicked,
										Stickied,
										Author,
										Score,
										ApprovedBy,
										Over18,
										Hidden,
										Thumbnail,
										SubredditID,
										Edited,
										LinkFlairCssClass,
										AuthorFlairCssClass,
										Downs,
										IsSelf,
										Permalink,
										Name,
										Created,
										URL,
										AuthorFlairText,
										Title,
										CreatedUTC,
										LinkFlairText,
										Ups,
										NumComments,
										NumReports,
										Distinguished,
										MediaEmbed,
										Media,
										CreatedBy,
										DateCreated,
										DateDeleted,
										VersionUser,
										VersionDate)
		OUTPUT Inserted.DataID INTO @tmp
		VALUES (@Domain,
				@BannedBy,
				@Subreddit,
				@SelfTextHtml,
				@SelfText,
				@Likes,
				@SecureMedia,
				@Saved,
				@ID,
				@SecureMediaEmbed,
				@Clicked,
				@Stickied,
				@Author,
				@Score,
				@ApprovedBy,
				@Over18,
				@Hidden,
				@Thumbnail,
				@SubredditID,
				@Edited,
				@LinkFlairCssClass,
				@AuthorFlairCssClass,
				@Downs,
				@IsSelf,
				@Permalink,
				@Name,
				@Created,
				@URL,
				@AuthorFlairText,
				@Title,
				@CreatedUTC,
				@LinkFlairText,
				@Ups,
				@NumComments,
				@NumReports,
				@Distinguished,
				@MediaEmbed,
				@Media,
				SYSTEM_USER,
				GETDATE(),
				NULL,
				SYSTEM_USER,
				GETDATE());

		SELECT @TID = pk FROM @tmp

		/***  Logging  ***/
		SELECT @datacounter = @@ROWCOUNT;
		SET @report = (SELECT	'Data Created'	AS LogMessage,
								GETDATE()		AS LogTimestamp,
								@datacounter	AS RowsAffected 
								FOR XML PATH('Notification'),
								TYPE,
								ROOT('Activity'));

		EXEC dbo.CreateLog @LogType = 'CREATE', @TableName = 'PostData', @Metadata = @report;
		RETURN 0;
	END TRY
	BEGIN CATCH
		DECLARE @ErrorMessage NVARCHAR(4000);
		DECLARE @ErrorSeverity INT;
		DECLARE @ErrorState INT;
		DECLARE @ErrorTimeStamp DATETIME;
		DECLARE @ErrAction NVARCHAR(25);

		SELECT	@ErrorMessage   = ERROR_MESSAGE(),
				@ErrorSeverity  = ERROR_SEVERITY(),
				@ErrorState     = ERROR_STATE(),
				@ErrorTimeStamp = GETDATE(),
				@ErrAction      = 'CREATE';

		SET @report = (SELECT	@ErrorMessage	AS ErrorMessage,
								@ErrAction	AS ErrorAction,
								@ErrorSeverity	AS ErrorSeverity,
								@ErrorState		AS ErrorState,
								@ErrorTimeStamp AS ErrorTimestamp 
								FOR XML PATH('Error'), 
								TYPE, 
								ROOT('ErrorLog'));		

		EXEC dbo.CreateErrorLog @ErrorType = 'ERROR', @ErrorAction = @ErrAction, @TableName = 'PostData', @Metadata = @report;
	END CATCH
END